/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.plan.web;

import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.common.util.ListUtil;
import com.javaeye.plan.dto.InStoreVoucher;
import com.javaeye.plan.dto.InStoreVoucherDetail;

public class InStoreVoucherCheckAction extends InStoreVoucherAction {
	
	protected static Log log = LogFactory.getLog(InStoreVoucherCheckAction.class); 
	
	private List<Float> acceptNumbers;
	
	private List<Float> otherNumbers;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3216585574200112708L;
	
	public String queryInStoreVoucherWaitCheckList() throws Exception {
		setDepartmentList(ListUtil.baseCodeList(codeService, ListUtil.UNIT_CODE_DEPATMENT));
		VoucherCondition condition = getCondition();
		condition.setStatus(InStoreVoucher.VOUCHES_STATES_CREATED);
		setVoucherList(service.getVoucherList(condition, getPageInfo()));
		return SUCCESS;
	}
	
	public String queryInStoreVoucherForCheck() throws Exception {
		setVoucher(service.getInStoreVoucher(getVoucherId()));
		return SUCCESS;
	}
	
	public String saveInStoreVoucherCheckResult() throws Exception {
		List<Integer> detailIds = getDetailIds();
		String voucherId = getVoucherId();
		
		InStoreVoucher pageVoucher = getVoucher();
		InStoreVoucher voucher = service.getInStoreVoucher(voucherId);
		voucher.setCheckDate(pageVoucher.getCheckDate());
		voucher.setCheckNo(pageVoucher.getCheckNo());
		voucher.setCheckResult(pageVoucher.getCheckResult());
		voucher.setChecker(pageVoucher.getChecker());
		voucher.setCheckDescribe(pageVoucher.getCheckDescribe());
		
		Set<InStoreVoucherDetail> details = voucher.getVoucherDetails();
		for (int i = 0; i < detailIds.size(); i++) {
			setDetailNumber(i, detailIds, details);
		}
		service.saveInStoreVoucher(voucher);
		return SUCCESS;
	}
	
	private void setDetailNumber(int index, List<Integer> detailIds, Set<InStoreVoucherDetail> details) {
		int detailId = detailIds.get(index);
		for (InStoreVoucherDetail detail : details) {
			if (detail.getId() == detailId) {
				detail.setAcceptNumber(acceptNumbers.get(index));
				detail.setOtherNumber(otherNumbers.get(index));
				break;
			}
		}
	}

	public void setAcceptNumbers(List<Float> acceptNumbers) {
		this.acceptNumbers = acceptNumbers;
	}

	public List<Float> getAcceptNumbers() {
		return acceptNumbers;
	}

	public void setOtherNumbers(List<Float> otherNumbers) {
		this.otherNumbers = otherNumbers;
	}

	public List<Float> getOtherNumbers() {
		return otherNumbers;
	}
}
